﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;

namespace CoastalClimbing.Auth
{
    public partial class Setters : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                try
                {
                    string action = Request.QueryString["action"].ToString();
                    string message = string.Empty;

                    switch (action)
                    {
                        case "d":
                            message = "Record successfully deleted.";
                            break;
                        case "a":
                            message = "Record successfully added.";
                            break;
                        case "e":
                            message = "Record successfully edited.";
                            break;
                        default:
                            break;
                    }

                    lblMessage.Text = message;
                }
                catch { }

                BindInfo();
            }
        }

        private void BindInfo()
        {
            grvSetter.DataSource = DAL.SetterSelect();
            grvSetter.DataBind();
        }

        protected void CancelSetter(object sender, GridViewCancelEditEventArgs e)
        {
            grvSetter.EditIndex = -1;
            this.BindInfo();
        }

        protected void EditSetter(object sender, GridViewEditEventArgs e)
        {
            grvSetter.EditIndex = e.NewEditIndex;
            this.BindInfo();
        }

        protected void UpdateSetter(object sender, GridViewUpdateEventArgs e)
        {
            string id = grvSetter.DataKeys[e.RowIndex].Value.ToString();

            GridViewRow row = grvSetter.Rows[e.RowIndex];
            TextBox txtgName = (TextBox)row.FindControl("txtSetterName");

            if (!string.IsNullOrWhiteSpace(txtgName.Text))
            {
                DAL.SetterUpdate(id, txtgName.Text);
            }

            grvSetter.EditIndex = -1;

            Response.Redirect("~/Auth/Routes.aspx?action=e");
        }

        protected void btnAddSetter_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtSetter.Text))
            {
                DAL.SetterCreate(txtSetter.Text);
                Response.Redirect("~/Auth/Routes.aspx?action=a");
            }
        }

        protected void DeleteSetter(object sender, GridViewDeleteEventArgs e)
        {
            string id = grvSetter.DataKeys[e.RowIndex].Value.ToString();
            DAL.SetterDelete(id);
            Response.Redirect("~/Auth/Routes.aspx?action=d");
        }

        protected void SetterDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this route?')");
            }
        }
    }
}